﻿using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
using SqLiteEditor.Helper;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;

namespace SqLiteEditor.Forms
{
    /// <summary>
    /// Interaction logic for FrmAddIndex.xaml
    /// </summary>
    public partial class FrmAddIndex : Window
    {
        
        public FrmAddIndex()
        {
            InitializeComponent();

            DMLEdittor.Clear();

            mTablesList = new ObservableCollection<string>();
            mFieldsList = new ObservableCollection<string>();

            TablesList.CollectionChanged += ticketsList_CollectionChanged;

            this.DataContext = this;
        }

        private ObservableCollection<string> mTablesList;
        public ObservableCollection<string> TablesList
        {
            get { return mTablesList; }
            set { mTablesList = value; }
        }

        private ObservableCollection<string> mFieldsList;
        public ObservableCollection<string> FieldsList
        {
            get { return mFieldsList; }
            set { mFieldsList = value; }
        }

        private string mTableName;
        public string TableName
        {
            get { return mTableName; }
            set { mTableName = value; }
        }


        private string mColumnName;
        public string ColumnName
        {
            get { return mColumnName; }
            set { mColumnName = value; }
        }

        public FrmAddIndex(List<string> tablename) : this()
        {
            foreach (var item in tablename) TablesList.Add(item);
        }

        public string IndexName
        {
            get { return (string)GetValue(IndexNameProperty); }
            set { SetValue(IndexNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TableName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IndexNameProperty =
            DependencyProperty.Register("IndexName", typeof(string), typeof(FrmAddTable), new PropertyMetadata("Idx_Index_", OnPropertyChanged));

        private static void OnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            FrmAddIndex frmAddIndex = (sender as FrmAddIndex);
            frmAddIndex.UpdateQuery();
        }

        private void UpdateQuery()
        {
           
        }

        void ticketsList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
                       
            UpdateQuery();
        }


        private void InitializeSyntaxHighlighting()
        {
            using (Stream s = TStreamHelper.OpenStream("Sql.xshd"))
            {
                using (XmlTextReader reader = new XmlTextReader(s))
                {
                    DMLEdittor.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
                }
            }

        }

        private void SqliteStructureDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            UpdateQuery();
        }

        public string ResultQuery { get; set; }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {

            string query = DMLEdittor.Text;

            if (String.IsNullOrEmpty(query))
            {
                MessageBox.Show("Please Press generate Query");
                return;
            }

            ResultQuery = DMLEdittor.Text;
            this.DialogResult = true;
            
            Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            ResultQuery = "";
            this.DialogResult = false;
            Close();
        }

        private void cmb_Tables_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string SelectedTableName = (string)cmb_Tables.SelectedItem;

            if (String.IsNullOrEmpty(SelectedTableName)) return;

            TableName = SelectedTableName;

            List<string> fields = SqliteHelper.GetColumnFieldsNames(SelectedTableName);

            if (fields == null) return;

            foreach (var item in fields) FieldsList.Add(item);
        }

        private void cmb_columnName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string SelectedColumn = (string)cmb_columnName.SelectedItem;

            if (String.IsNullOrEmpty(SelectedColumn)) return;

            ColumnName = SelectedColumn;
        }

        private void btnCreateIndexQuery_Click(object sender, RoutedEventArgs e)
        {
            List<string> fields = new List<string>();
           fields.Add(ColumnName);
           string qurey = SqliteHelper.Query_AddIndex(txt_IndexName.Text, fields, ch_IsUniqe.IsChecked.Value, TableName);
           DMLEdittor.Text = qurey;
        }

       
    }
}
